package com.berje.vkalarm;

import android.app.Activity;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.berje.vkalarm.vksdk.Api;
import com.berje.vkalarm.vksdk.model.Audio;
import com.berje.vkalarm.vksdk.model.GroupAudioFeed;
import com.berje.vkalarm.vksdk.model.GroupAudioNewsItem;

import java.util.List;

public class AlarmActivity extends Activity {
    private static final String TAG = "vkalarm-alarm";
    private Account account = new Account();
    private Api api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);

        account.restore(this);
        if (account.getAccess_token() != null) {
            Log.d(TAG, "Saved token: " + account.getAccess_token() + " " + account.getUser_id());
            api = new Api(account.getAccess_token(), Constants.API_ID);

            Intent intent = getIntent();
            long lgid = intent.getLongExtra("gid", 0);

            if (lgid != 0) {
                String gid = "g" + lgid;
                Log.d(TAG, "intent gid: " + gid);

                new AsyncTask<String, Void, List<Audio>>() {

                    @Override
                    protected List<Audio> doInBackground(String... params) {
                        String gid = params[0];
                        List<Audio> ret = null;
                        try {
                            GroupAudioFeed gmf = api.getGroupMusicNews(gid, -1, 0, -1, "", "");
                            Log.d(TAG, "feed: " + gmf);
                            List<GroupAudioNewsItem> items = gmf.getItems();

                            Audio sa = null;
                            for (GroupAudioNewsItem item : items) {
                                List<Audio> audios = item.getAudios();
                                if (audios != null && audios.size() > 0) {
                                    sa = audios.get(0);
                                    break;
                                }
                            }

                            Log.d(TAG, "sa: " + sa);
                            ret = api.getAudioById(sa.getId());
                            Log.d(TAG, "audios: " + ret);
                        } catch (Exception e) {
                            Log.e(TAG, "Exception getting feed", e);
                        }
                        return ret;
                    }

                    @Override
                    protected void onPostExecute(List<Audio> audios) {
                        super.onPostExecute(audios);
                        if (audios != null) {
                            String mp3uri = audios.get(0).getUrl();
                            Log.d(TAG, "mp3Uri: " + mp3uri);
                            Uri uri = Uri.parse(mp3uri);
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                        } else {
                            defaultAlarm();
                        }

                    }
                }.execute(gid);

            }

        } else {
            Log.d(TAG, "No vk auth info in shared");
        }
    }

    private void defaultAlarm() {
        Log.d(TAG, "default_alarm");

        Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Log.d(TAG, "TYPE_ALARM: " + alert);
        if(alert == null){
            // alert is null, using backup
            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Log.d(TAG, "TYPE_NOTIFICATION: " + alert);
            if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
                // alert backup is null, using 2nd backup
                alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                Log.d(TAG, "TYPE_RINGTONE: " + alert);
            }
        }
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), alert);
        r.play();
    }
}
